using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public enum TankType
{ 

    Player = 0,
    Player2 = 1,
    Enemy1 = 2,
    Enemy2 = 3,
    Enemy3 = 4,
}
public enum GameState
{ 
      Ready=0,
      Running=1,
      Pause=2,
      GameOver=3,
      GameWin=4,

}

public class GameManager : MonoBehaviour
{
    public float x_lenth = 10.5f;
    public float y_lenth = 8f;

    public GameObject AriWall;
    public GameObject Maps;

    public GameObject born;
    public GameObject[] bornPos;

    public TankType enemyTankType = TankType.Enemy1;
    public Vector3 enemyTankPos = Vector3.zero;

    
    public float enemyBornTimeInterval = 2f;

    public int MaxEnemyCount = 8;
    public int RestEnemyCount = 12;
    public int CurrentEnemyCount = 0;
    public int PlayerCount = 3;
    public int PlayerCounts = 3;

    public float invokeTime = 0.5f;

    public static GameManager gameManager_Instance;

    public GameState game_State = GameState.Ready;
    public GameObject gameOver;
    public GameObject playerover;
    public GameObject playerwin;

    public GameObject star;
    public Vector3 starPos = Vector3.zero;
    public float bornTime = 5f;
    public int StarCount = 0;

    public Vector2 p3;
    public bool mjudgment=false;
    public bool mjudgment1 = false;
    //public SpriteRenderer EnemyRenderer;
    //public Sprite Enemyph1;
    //public Sprite Enemyph2;
    //public Sprite Enemyph3;
    // Start is called before the first frame update

    //rank
    public bool rankbool=false;
    public bool bossbool = false;
    void Awake()
    {
        gameManager_Instance = this;
    }

    void Start()
    {
        game_State = GameState.Running;
        CreateAriWall();
        CreatePlayer();
        if (tankstart.tankstart_Instance.Startwo) { 
            CreatePlayers();}
        
        //CreateEnemy();
        InvokeRepeating("CreateEnemy",0.1f, enemyBornTimeInterval);
        if (gameOver != null) 
        {
            gameOver.SetActive(false);
            playerover.SetActive(false);
            playerwin.SetActive(false);
        }
        InvokeRepeating("win", bornTime, 1f);
        InvokeRepeating("CreateStar", bornTime, bornTime);
        //InvokeRepeating("ttk", 1f, 1f);
        Cursor.visible = false;
        
    }

    // Update is called once per frame
    void Update()
    {
        if (GameManager.gameManager_Instance.game_State == GameState.GameOver || GameManager.gameManager_Instance.game_State == GameState.GameWin) 
        {
            if (Input.GetKeyDown(KeyCode.Return)) 
            {
                GameRestart();
            }
           
        }

        if (GameManager.gameManager_Instance.game_State == GameState.GameOver&& rankbool==false)
        {
            Cursor.visible = true;
            if (gameOver != null)
            {
                playerover.SetActive(true);
                gameOver.SetActive(true);
                
                ObjectPool.instance = null;
                
            }
        }
       
        //print(Camera.main.ScreenToWorldPoint(Input.mousePosition));
    }
    void CreateAriWall()
    {
        Vector3 pos1;
        Vector3 pos2;
        Vector3 pos3;
        Vector3 pos4;

        for (float i = 0; i <= x_lenth; i++)
        {
            pos1 = new Vector3(i + 0.5f, -(y_lenth + 0.5f), 0);
            pos2 = new Vector3(-(i + 0.5f), -(y_lenth + 0.5f), 0);
            pos3 = new Vector3(i + 0.5f, (y_lenth + 0.5f), 0);
            pos4 = new Vector3(-(i + 0.5f), (y_lenth + 0.5f), 0);

            if (AriWall != null)
            {
                GameObject AriWall1 = Instantiate(AriWall, pos1, AriWall.transform.rotation);
                GameObject AriWall2 = Instantiate(AriWall, pos2, AriWall.transform.rotation);
                GameObject AriWall3 = Instantiate(AriWall, pos3, AriWall.transform.rotation);
                GameObject AriWall4 = Instantiate(AriWall, pos4, AriWall.transform.rotation);
                AriWall1.transform.SetParent(Maps.transform);
                AriWall2.transform.SetParent(Maps.transform);
                AriWall3.transform.SetParent(Maps.transform);
                AriWall4.transform.SetParent(Maps.transform);
            }
        }

        for (float j = 0; j <= y_lenth; j++)
        {
             pos1 = new Vector3(x_lenth + 0.5f,(j + 0.5f), 0);
             pos2 = new Vector3(x_lenth + 0.5f,-(j + 0.5f), 0);
             pos3 = new Vector3(-(x_lenth + 0.5f),(j + 0.5f), 0);
             pos4 = new Vector3(-(x_lenth + 0.5f),-(j + 0.5f), 0);

            if (AriWall != null)
            {
                GameObject AriWall1 = Instantiate(AriWall, pos1, AriWall.transform.rotation);
                GameObject AriWall2 = Instantiate(AriWall, pos2, AriWall.transform.rotation);
                GameObject AriWall3 = Instantiate(AriWall, pos3, AriWall.transform.rotation);
                GameObject AriWall4 = Instantiate(AriWall, pos4, AriWall.transform.rotation);
                AriWall1.transform.SetParent(Maps.transform);
                AriWall2.transform.SetParent(Maps.transform);
                AriWall3.transform.SetParent(Maps.transform);
                AriWall4.transform.SetParent(Maps.transform);
            }
        }

    }

   public void CreatePlayer()
    {
        if (PlayerCount <= 0) return;
        //mjudgment =true;
        mjudgment1 = true;
        if (born != null && bornPos.Length >= 4 && bornPos[0] != null)
        {
            var obj = Instantiate(born,bornPos[0].transform.position,born.transform.rotation);
            obj.GetComponent<Born>().tankType = TankType.Player;
        }
        PlayerCount--;
        //Debug.Log(PlayerCount);
    }

    public void CreatePlayers()
    {
        if (PlayerCounts <= 0) return;

        if (born != null && bornPos.Length >= 4 && bornPos[0] != null)
        {
            var obj = Instantiate(born, bornPos[0].transform.position, born.transform.rotation);
            obj.GetComponent<Born>().tankType = TankType.Player2;
        }
        PlayerCounts--;
       //Debug.Log(PlayerCounts);
    }
    public void ReCreatePlayers()
    {
        Invoke("CreatePlayers", invokeTime);
    }

    public void ReCreatePlayer() 
    {
        Invoke("CreatePlayer", invokeTime);
    }

    public void Enemyjian() 
    {
        CurrentEnemyCount--;
        texttime.texttime_Instance.Enmeynumbers=true;
    }
    public void Enemybos()
    {
        //EnemyRenderer.sprite = Enemyph1;
    }

    void CreateEnemy()
    {
        if (RestEnemyCount <= 0) return;
        if (CurrentEnemyCount >= MaxEnemyCount) return;
        float randomEnemy = Random.Range(0, 6);
        float randomPos = Random.Range(0, 6);

        if (randomEnemy <= 1)
        {

            enemyTankType = TankType.Enemy1;
        }


        if (randomEnemy >1&& randomEnemy <=3)
        {

            enemyTankType = TankType.Enemy2;
        }


        if (randomEnemy >3&& randomEnemy<=6)
        {

            enemyTankType = TankType.Enemy3;
        }

        if (bornPos.Length < 4) return;
        if (randomPos <= 2)
        {
            if (bornPos[1] == null) return;
            enemyTankPos = bornPos[1].transform.position;
        }


        if (randomPos > 2 && randomPos <= 4)
        {
            if (bornPos[2] == null) return;
            enemyTankPos = bornPos[2].transform.position;
        }


        if (randomPos > 4 && randomPos <= 6)
        {
            if (bornPos[3] == null) return;
            enemyTankPos = bornPos[3].transform.position;
        }

        var obj = Instantiate(born, enemyTankPos, born.transform.rotation);
        obj.GetComponent<Born>().tankType = enemyTankType;
        CurrentEnemyCount++;
        RestEnemyCount--;
    }

    public void GameOvers()
    {
        //if (PlayerCount == 0) 
        //{
        //    PlayerCount--;
        //}

        //if (PlayerCounts == 0)
        //{
        //    PlayerCounts--;
        //}

        if (PlayerCount==-1&& PlayerCounts==-1)
        {
            game_State = GameState.GameOver;

            //if (gameOver != null)
            //{
            //    playerover.SetActive(true);
            //    gameOver.SetActive(true);
            //}
        }
    }

    public void GameOver()
    {
        if (PlayerCount == -1)
        {
            game_State = GameState.GameOver;

            //if (gameOver != null)
            //{
            //    playerover.SetActive(true);
            //    gameOver.SetActive(true);
            //}
        }
    }
    public void GameOverp()
    {
        //if (CurrentEnemyCount == 1&&Enemys.Enemy_Instances.PH==0f)
        //{
        //    game_State = GameState.GameOver;

        //    if (gameOver != null)
        //    {
        //        gameOver.SetActive(true);
        //    }
        //}
    }

    void GameRestart() 
    {
        SceneManager.LoadScene("start");
    }

    void CreateStar()
    {
        if (StarCount >= 1) return;
        float x = Random.Range(-(x_lenth-0.5f), x_lenth - 0.5f);
        float y = Random.Range(-(y_lenth-0.5f), y_lenth - 0.5f);
        starPos = new Vector3(x,y,0);
        if (star!=null)
        {
            Instantiate(star,starPos,star.transform.rotation);
        }
        StarCount++;

    }
    void win() 
    {
        if (GameManager.gameManager_Instance.game_State == GameState.GameOver) { return; }
            if (CurrentEnemyCount == 0)
        {
            
            
            if (!tankstart.tankstart_Instance.Startwo&&rankbool==false)
            {
                
                
                bossbool = true;
                if (FSM.FSM_Instance.parameter.IsDeath) {
                game_State = GameState.GameWin;
                ObjectPool.instance = null;
                rankbool = true;
                texttime.texttime_Instance.stopco();
                if (gameOver != null)
                {

                    gameOver.SetActive(true);
                    playerwin.SetActive(true);

                }
                }
            }
            if (tankstart.tankstart_Instance.Startwo && rankbool == false)
            {game_State = GameState.GameWin;
                rankbool = true;
                if (gameOver != null)
                {

                    gameOver.SetActive(true);
                    playerwin.SetActive(true);

                }
            }
            
        }
    }
    
}

